#!/bin/bash
# 脚本待验证

# 对于CentOS/RHEL系统
# sudo yum install mailx

#!/bin/bash

# 邮件变量配置
EMAILS="example1@example.com,example2@example.com" # 收件人邮箱地址，使用逗号分隔多个邮箱
SUBJECT="Daily System Health Check Report"
FROM="sender@example.com" # 发件人邮箱地址
SMTP_SERVER="smtp.example.com" # SMTP服务器地址
SMTP_PORT="587" # SMTP服务器端口
SMTP_USERNAME="your_username" # SMTP登录用户名
SMTP_PASSWORD="your_password" # SMTP登录密码

# 创建临时文件存储检查结果
TEMP_FILE=$(mktemp)

# 系统健康检查内容
echo "Disk Usage:" >> $TEMP_FILE
df -h | grep -v tmpfs >> $TEMP_FILE 2>&1
echo "" >> $TEMP_FILE

echo "Memory Usage:" >> $TEMP_FILE
free -m >> $TEMP_FILE 2>&1
echo "" >> $TEMP_FILE

echo "System Load:" >> $TEMP_FILE
uptime >> $TEMP_FILE 2>&1
echo "" >> $TEMP_FILE

# 使用mail命令发送邮件
# 注意：这里假设使用的是支持SMTP认证的mail版本
(
  echo "From: $FROM"
  echo "To: $EMAILS" # 多个收件人邮箱
  echo "Subject: $SUBJECT"
  echo "Content-Type: text/plain; charset=UTF-8"
  echo ""
  cat $TEMP_FILE
) | /usr/sbin/sendmail -t -S "$SMTP_SERVER:$SMTP_PORT" -am -au"$SMTP_USERNAME" -ap"$SMTP_PASSWORD"

# 删除临时文件
rm $TEMP_FILE

echo "Report sent to $EMAILS"